﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MWTWorkUnitMgmtTool.DataAccess.TableGatewayRepository;
using MWTWorkUnitMgmtTool.BusinessEntities;

namespace MWTWorkUnitMgmtTool.Services
{
    public class MappingService:IMappingService
    {
        private IMappingRepository _mappingRepository; 
        public MappingService(IMappingRepository mappingRepository)
        {
            _mappingRepository = mappingRepository; 
        }
        #region IMappingService Members

        public IEnumerable<BusinessEntities.Mapping> GetMappingsByBOM(string itemCode)
        {
            return _mappingRepository.GetMappingsByBOM(itemCode).Select(f => ObjectFactories.ObjectFactory.MappingFactory(f)); 
        }

        public int InsertMapping(string workUnitCode, string locationCode, string itemCode)
        {
            //return _mappingRepository.InsertMapping(workUnitCode, locationCode, itemCode); 
            Mapping mapping = new Mapping();
            mapping.WorkUnitCode = workUnitCode;
            mapping.LocationCode = locationCode;
            mapping.ItemCode = itemCode;

            return mapping.InsertMapping(); 
        }

        public int UpdateMapping(string workUnitCode, string locationCode, string itemCode)
        {
            return _mappingRepository.UpdateMapping(workUnitCode, locationCode, itemCode); 
        }

        public int DeleteMapping(string workUnitCode, string locationCode, string itemCode)
        {
            return _mappingRepository.DeleteMapping(workUnitCode, locationCode, itemCode);
        }

        public Mapping GetMappingsByID(int id)
        {
            return _mappingRepository.GetMappingsByID(id).Select(f => ObjectFactories.ObjectFactory.MappingFactory(f)).ElementAtOrDefault(0);
        }

        public void UploadMappings(string filePath)
        {
            _mappingRepository.UploadMappingsToDB(filePath);
        }

        public IEnumerable<Mapping> GetAllMappings()
        {
            return _mappingRepository.GetAllMappings().Select(f => ObjectFactories.ObjectFactory.MappingFactory(f)); 
        }

        
        #endregion



    }
}
